/*
 * Atualizado por Davi Uezono em 12.10.2011 às 03h09min.
 */
package Project;
import java.lang.String.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;
import Objects.*;

/**
 *
 * @author Felipe
 */
public class NewSerpent extends javax.swing.JPanel {
    public static final String NAME = "Serpent";
    private SnakesCRUD parent;
    
    public NewSerpent(SnakesCRUD parent) {
    this.parent = parent;
    initComponents();
  }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Add = new javax.swing.JButton();
        Remove = new javax.swing.JButton();
        Refresh = new javax.swing.JButton();
        edSpecie = new javax.swing.JTextField();
        edMemberID = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        edSerpentID = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        Modify = new javax.swing.JButton();
        edRegionID = new javax.swing.JTextField();

        Add.setText("Add");
        Add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddActionPerformed(evt);
            }
        });

        Remove.setText("Remove");
        Remove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RemoveActionPerformed(evt);
            }
        });

        Refresh.setText("Refresh");
        Refresh.setToolTipText("");

        edMemberID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                edMemberIDActionPerformed(evt);
            }
        });

        jLabel1.setText("Serpent ID");

        jLabel2.setText("Specie");
        jLabel2.setToolTipText("");

        jLabel3.setText("RegionID");

        jLabel9.setText("MemberID");

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel10.setText("Cadastro de novas serpentes:");

        jLabel11.setForeground(new java.awt.Color(255, 0, 0));
        jLabel11.setText("Obs: Se a espécie for indefinida, use código 999.");

        Modify.setText("Modify");
        Modify.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ModifyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(65, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel9)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(edRegionID, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                            .addComponent(edSpecie, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                            .addComponent(edSerpentID, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                            .addComponent(edMemberID, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE))
                        .addGap(72, 72, 72))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Add, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(Modify, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24)
                        .addComponent(Remove)
                        .addGap(18, 18, 18)
                        .addComponent(Refresh)
                        .addContainerGap(35, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addContainerGap(156, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(edSpecie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(edSerpentID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(edMemberID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(edRegionID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                .addComponent(jLabel11)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Remove)
                    .addComponent(Add)
                    .addComponent(Modify)
                    .addComponent(Refresh))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void ModifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ModifyActionPerformed
    if (!edMemberID.getText().isEmpty() && !edSpecie.getText().isEmpty() && !edRegionID.getText().isEmpty() && !edSerpentID.getText().isEmpty()) 
    {  
      try {
        Serpent s = new Serpent(edSpecie.getText(), Integer.parseInt(edMemberID.getText()), Integer.parseInt(edSerpentID.getText()),
                Integer.parseInt(edRegionID.getText()));
        
        //</editor-fold>
        parent.executeCommand(s.toUpdate());
        JOptionPane.showMessageDialog(null, "Data updated!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch(SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
    }
    else {
        JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_ModifyActionPerformed

private void RemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RemoveActionPerformed
if (!edMemberID.getText().isEmpty()) 
    {  
      try {
        Serpent s = new Serpent(edSpecie.getText(), Integer.parseInt(edMemberID.getText()), Integer.parseInt(edSerpentID.getText()),
                Integer.parseInt(edRegionID.getText()));
        
        //</editor-fold>
        parent.executeCommand(s.toDelete());
        JOptionPane.showMessageDialog(null, "Data deleted!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch(SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
    }
    else {
        JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.ERROR_MESSAGE);
    }
    
    
    
}//GEN-LAST:event_RemoveActionPerformed

private void AddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddActionPerformed
if (!edMemberID.getText().isEmpty() && !edSpecie.getText().isEmpty() && !edRegionID.getText().isEmpty() && 
            !edSerpentID.getText().isEmpty()) 
    {  
      try {
        Serpent s = new Serpent(edSpecie.getText(), Integer.parseInt(edMemberID.getText()), Integer.parseInt(edSerpentID.getText()),
                Integer.parseInt(edRegionID.getText()));
        
        //</editor-fold>
        parent.executeCommand(s.toInsert());
        JOptionPane.showMessageDialog(null, "Data inserted!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch(SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
    }
    else {
        JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.ERROR_MESSAGE);
    }
    
    
    
}//GEN-LAST:event_AddActionPerformed

private void edMemberIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_edMemberIDActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_edMemberIDActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Add;
    private javax.swing.JButton Modify;
    private javax.swing.JButton Refresh;
    private javax.swing.JButton Remove;
    private javax.swing.JTextField edMemberID;
    private javax.swing.JTextField edRegionID;
    private javax.swing.JTextField edSerpentID;
    private javax.swing.JTextField edSpecie;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel9;
    // End of variables declaration//GEN-END:variables
}
